package com.car.advertising.view;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.car.advertising.CarApplication;
import com.car.advertising.R;
import com.car.advertising.activity.AboutBoMeiActivity;
import com.car.advertising.activity.CarOwnerMessageActivity;
import com.car.advertising.activity.FeedbackActivity;
import com.car.advertising.activity.LoginActivity;
import com.car.advertising.activity.UpdatePasswordActivity;
import com.car.advertising.view.viewAdapter.NavigationMenuAdapter;
import com.car.advertising.connection.Contact;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.entity.MeEntity;
import com.support.widget.JumpManager;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.layout.view.RecycleViewDivider;
import com.support.widget.network.BaseResponse;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.LogUtil;
import com.support.widget.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 开发者：LuoYi
 * Time: 2017 10:47 2017/4/11 04
 */

public class NavigationMenu extends RelativeLayout {

    LayoutInflater mLayoutInflater;
    RecyclerView mlistView1;
    private List<MeEntity> menuItemsDataSource = new ArrayList<>();
    private DrawerLayout mDrawerLayout;// 关联到的抽屉控件
    private String loginStatus;
    private CustomDialog customDialog;
    private NavigationMenuAdapter menuAdapter;

    public NavigationMenu(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initLayout(context);
    }

    public NavigationMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLayout(context);
    }

    public NavigationMenu(Context context) {
        super(context);
        initLayout(context);
    }

    private void initLayout(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
        View contentView = mLayoutInflater.inflate(R.layout.navigation_menu, null);
        RelativeLayout.LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(contentView, lp);

        mlistView1 = (RecyclerView) contentView.findViewById(R.id.home_menu_lv);
        mlistView1.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.c_eae8e8)));

        setListData();
    }

    private void setListData() {
        menuItemsDataSource.clear();
        loginStatus = CarApplication.token;
        String[] array = getResources().getStringArray(R.array.navigation_menu_items_array);
        for (int i = 0; i < array.length; i++) {
            menuItemsDataSource.add(new MeEntity(i, null, array[i], true, null));
        }
        if (!TextUtils.isEmpty(loginStatus)) {
            menuItemsDataSource.add(new MeEntity(menuItemsDataSource.size() + 1, null, "退出登录", true, null));
        } else {
            menuItemsDataSource.add(new MeEntity(menuItemsDataSource.size() + 1, null, "立即登录", true, null));
        }
        setListAdapter();
    }

    private void setListAdapter() {
        if (menuAdapter == null) {
            menuAdapter = new NavigationMenuAdapter(getContext(), menuItemsDataSource);
            mlistView1.setAdapter(menuAdapter);
            mlistView1.setLayoutManager(new LinearLayoutManager(getContext()));
            menuAdapter.setOnItemClickListener(drawerItemClickListener);
        } else {
            menuAdapter.setRefreshList(menuItemsDataSource);
        }

    }

    /**
     * 包含 的 listView的点击事件
     *
     * @author Luoyi
     */
    private OnRecyclerViewItemClickListener drawerItemClickListener = new OnRecyclerViewItemClickListener() {

        @Override
        public void onItemClick(ViewGroup parent, View view, int position) {
            switch (position) {
                case 0:// 密码管理
                    if (!TextUtils.isEmpty(loginStatus)) {
                        JumpManager.gotoActivity(getContext(), UpdatePasswordActivity.class, false);
                        hide();
                    } else {
                        ToastUtil.show(getContext(), "请先登录后再尝试", Toast.LENGTH_SHORT);
                    }
                    break;
                case 1:// 关于博媒
                    JumpManager.gotoActivity(getContext(), AboutBoMeiActivity.class, false);
                    hide();
                    break;
                case 2:// 意见反馈
                    if (!TextUtils.isEmpty(loginStatus)) {
                        JumpManager.gotoActivity(getContext(), FeedbackActivity.class, false);
                        hide();
                    } else {
                        ToastUtil.show(getContext(), "请先登录后再尝试", Toast.LENGTH_SHORT);
                    }
                    break;
                case 3:// 帮助说明
                    ToastUtil.show(getContext(), "帮助说明", Toast.LENGTH_SHORT);
                    hide();
                    break;
                case 4:// 邀请有奖
                    ToastUtil.show(getContext(), "邀请有奖", Toast.LENGTH_SHORT);
                    hide();
                    break;
                case 5:// 车主认证
                    if (!TextUtils.isEmpty(loginStatus)) {
                        JumpManager.gotoActivity(getContext(), CarOwnerMessageActivity.class, false);
                        hide();
                    } else {
                        ToastUtil.show(getContext(), "请先登录后再尝试", Toast.LENGTH_SHORT);
                    }
                    break;
                case 6:// 退出登录
                    if (!TextUtils.isEmpty(loginStatus)) {
                        setCustomDialog("是否退出登录？");
                    } else {
                        JumpManager.gotoActivity(getContext(), LoginActivity.class, false);
                        hide();
                    }
                    break;

                default:
                    break;
            }
        }
    };

    public CharSequence getTitle() {
        return "导航菜单";
    }

    /**
     * 关联到 drawerLayout
     *
     * @param drawerLayout
     */
    public void attacthDrawer(DrawerLayout drawerLayout) {
        mDrawerLayout = drawerLayout;
    }

    /**
     * 隐藏
     */
    public void hide() {
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(NavigationMenu.this);
        }
    }

    /**
     * 出现
     */
    public void show() {
        if (mDrawerLayout != null) {
            mDrawerLayout.openDrawer(NavigationMenu.this);
        }
    }

    private void setCustomDialog(String titleStr) {
        CustomDialog.Builder builder1 = new CustomDialog.Builder(getContext());
        builder1.setTitle(titleStr);
        builder1.setPositiveButton("确定", new DialogClick('q'));
        builder1.setNegativeButton("取消", new DialogClick('c'));
        customDialog = builder1.create();
        customDialog.show();
    }

    class DialogClick implements DialogInterface.OnClickListener {

        char type;

        public DialogClick(char type) {
            this.type = type;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (type) {
                case 'q':
                    customDialog.dismiss();
                    setLogin();
                    hide();
                    break;
                case 'c':
                    customDialog.dismiss();
                    break;
                default:
                    break;
            }
        }
    }

    private void setLogin() {
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        params.add(new OkHttpClientManager.Param("acToken", CarApplication.token));
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_022, new OkHttpClientManager.ResultCallback<BaseResponse>() {
            @Override
            public void onError(String request) {
                LogUtil.i(request);
            }

            @Override
            public void onResponse(BaseResponse resp) {
                Contact.LoginAgain((Activity) getContext());
                setListData();
            }

            @Override
            public void onFinish() {

            }
        }, params, null);
    }

}
